package tornadoj.web;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import tornadoj.points.IExceptionPage;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Web异常捕捉过滤器
 * 1,javax.servlet.error.status_code
 * This attribute give status code which can be stored and analysed after storing in a java.lang.Integer data type.
 * 2,javax.servlet.error.exception_type
 * This attribute gives information about exception type which can be stored and analysed after storing in a java.lang.Class data type.
 * 3,javax.servlet.error.message
 * This attribute gives information exact error message which can be stored and analysed after storing in a java.lang.String data type.
 * 4,javax.servlet.error.request_uri
 * This attribute gives information about URL calling the servlet and it can be stored and analysed after storing in a java.lang.String data type.
 * 5,javax.servlet.error.exception
 * This attribute gives information the exception raised which can be stored and analysed after storing in a java.lang.Throwable data type.
 * 6,javax.servlet.error.servlet_name
 * This attribute gives servlet name which can be stored and analysed after storing in a java.lang.String data type.
 * <p/>
 * Created by tinyms on 14-2-11.
 */

@WebFilter(urlPatterns = "/*")
public class ExceptionFilter implements Filter {

    private final static Logger Log = LoggerFactory.getLogger(ExceptionFilter.class);

    public void init(FilterConfig filterConfig) throws ServletException {
    }

    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain){
        HttpServletResponse response = (HttpServletResponse) res;
        HttpServletRequest request = (HttpServletRequest) req;
        try {
            request.setCharacterEncoding("utf8");
            response.setCharacterEncoding("utf8");
            chain.doFilter(req, res);
            Throwable throwable = (Throwable) req.getAttribute("javax.servlet.error.exception");
            if (throwable != null) {
                List<IExceptionPage> items = ClassLoaderUtil.getPlugin(IExceptionPage.class);
                String url = "/error.jsp";
                for (IExceptionPage page : items) {
                    Map<String, Object> messages = new HashMap<String, Object>();
                    url = page.doInException(throwable, messages);
                    if (StringUtils.isNotBlank(url)) {
                        for (String key : messages.keySet()) {
                            request.setAttribute(key, messages.get(key));
                        }
                        break;
                    }
                }
                if(StringUtils.isNotBlank(url)){
                    Log.info(url);
                    request.getRequestDispatcher(url).forward(request, response);
                }
            }
        } catch (IOException e) {
            Log.error("IOException", e);
            e.printStackTrace();
        } catch (ServletException e) {
            Log.error("ServletException", e);
        }

    }


    public void destroy() {

    }
}
